//
//  MeSetNameView.m
//  Ginix
//
//  Created by ff on 2025/3/18.
//

#import "MeSetNameView.h"

@interface MeSetNameView()
@property (nonatomic,strong) UILabel * nameLabel;
@property (nonatomic,strong) UITextField * field;
@property (nonatomic,strong) UIButton * enterBtn;
@property (nonatomic,strong) UIButton * cancenBtn;
@property (nonatomic,assign) NSInteger  type;
@end

@implementation MeSetNameView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        self.location = CenterStyle;
        self.touchNoRespod = YES;
        [self setSubViewLayout];
    }
    return self;
}

-(void)setSubViewLayout{
    
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    self.contentView.frame = CGRectMake(40 *  FMScaleX, (HEIGHT - 200 * FMScaleX) / 2, WIDTH - 80 * FMScaleX, 200 * FMScaleX );
    [self.contentView setCornerRadius: 8 * FMScaleX];
    self.contentView.backgroundColor = UIColor.whiteColor;
    
    [self.contentView addSubview:self.nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(240 * FMScaleX, 30 * FMScaleX));
        make.centerX.equalTo(@0);
        make.top.mas_offset(24 * FMScaleX);
    }];
    [self.contentView addSubview:self.field];
    [_field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(240 * FMScaleX, 40 * FMScaleX));
        make.centerX.equalTo(@0);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(10 * FMScaleX);
    }];
    
    [self.contentView addSubview:self.cancenBtn];
    [_cancenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(@0);
        make.size.mas_offset(CGSizeMake(140 * FMScaleX, 48 * FMScaleX));
    }];
    [self.contentView addSubview:self.enterBtn];
    [_enterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.bottom.equalTo(self.cancenBtn);
        make.right.equalTo(@0);
    }];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self endEditing:YES];
}

-(void)startAnimationWithType:(NSInteger)type{
    
    [super startAnimation];

    if (type == 1 || type == 4) {
        self.field.keyboardType = UIKeyboardTypeDefault;
    }else{
        self.field.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
    }
        
    self.field.text = @"";
    self.type = type;
    switch (type) {
        case 1:
            self.nameLabel.text = @"修改昵称";
            self.field.attributedPlaceholder = ({
                NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"请输入昵称" attributes:@{NSForegroundColorAttributeName:RDRGB(0x999999),NSFontAttributeName:Regular(14)}];
                string;
            });
            break;
        case 2:
            self.nameLabel.text = @"修改身高";
            self.field.attributedPlaceholder = ({
                NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"请输入身高" attributes:@{NSForegroundColorAttributeName:RDRGB(0x999999),NSFontAttributeName:Regular(14)}];
                string;
            });
            break;
        case 3:
            self.nameLabel.text = @"修改体重";
            self.field.attributedPlaceholder = ({
                NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"请输入体重" attributes:@{NSForegroundColorAttributeName:RDRGB(0x999999),NSFontAttributeName:Regular(14)}];
                string;
            });
            break;
        case 4:
            self.nameLabel.text = @"修改戒指名称";
            self.field.attributedPlaceholder = ({
                NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"请输入戒指名称" attributes:@{NSForegroundColorAttributeName:RDRGB(0x999999),NSFontAttributeName:Regular(14)}];
                string;
            });
            break;
        default:
            break;
    }
    
    
}

-(void)cancenBtnAction{
    
    [self closeView];
    
}

-(void)enterBtnAction{
    
    if ([self.field.text isEqual:@""]) {
        FhudShowStr(@"请输入内容");
        return;
    }
    
    if (self.enterdBtnBlock) {
        self.enterdBtnBlock(self.field.text,self.type);
        [self closeView];
    }
}

-(UILabel *)nameLabel{
    
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = Medium(17);
        _nameLabel.textColor = RDRGB(0x171A1D);
        _nameLabel.text = @"是否绑定";
        _nameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _nameLabel;
}

-(UITextField *)field{
    
    if (!_field) {
        _field = [[UITextField alloc]init];
        _field.font = Regular(16);
        _field.textColor = RDRGB(0x666666);
        _field.textAlignment = NSTextAlignmentCenter;
        [_field setCornerRadius:5 * FMScaleX borderWidth:1 borderColor:RDRGB(0x714933)];
        
    }
    return _field;
}


-(UIButton *)cancenBtn{
    
    if (!_cancenBtn) {
        _cancenBtn = [[UIButton alloc]init];
        [_cancenBtn setTitle:@"取消" forState:UIControlStateNormal];
        _cancenBtn.titleLabel.font = Regular(17);
        [_cancenBtn setBackgroundColor:RDRGB(0xF7F6F9)];
        [_cancenBtn setTitleColor:RDRGB(0x666666) forState:UIControlStateNormal];
        [_cancenBtn addTarget:self action:@selector(cancenBtnAction ) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancenBtn;
}

-(UIButton *)enterBtn{
    
    if(!_enterBtn){
        _enterBtn = [[UIButton alloc]init];
        [_enterBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_enterBtn setTitleColor:RDRGB(0xAD7D62) forState:UIControlStateNormal];
        [_enterBtn addTarget:self action:@selector(enterBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _enterBtn;
    
}
@end
